package com.example.administrator.puzzlepic.com.xiaoxu.util;

import android.graphics.Bitmap;
import android.util.Log;

import com.example.administrator.puzzlepic.com.xiaoxu.util.ImageUtil;
import com.example.administrator.puzzlepic.com.xiaoxu.util.ItemBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2016/4/8.
 */
public class GameUtil {

    /**
     * 判断随机图是否有解
     */

//    public static Boolean canResult(List<Integer> data, int type) {
//
//        int sum = getSum(data);//倒置和
//        ItemBean itemBlank = ImageUtil.mBlankItemBean;
//        int mItemId = itemBlank.getmItemId();
//
//        if (data.size() % 2 == 1) {    //奇数列
//            if (sum % 2 == 0) {  //倒置和为偶
//                return true;
//            }
//
//        } else {
//            //序列A的 列数为偶数时
//
//           // int columns = getColumns(data, imageId, type);//得到当前空白图的列数(倒序)
//            //   Log.d("GameUtil", "columns当前的列数为(从下往上)" + columns);
//
//            if (((mItemId - 1) / type) % 2 == 1) {
//                if (sum % 2 == 0) {
//                    return true;
//                }
//
//
//            }
//
//           else{
//                if (sum % 2 == 1) {
//
//                    return true;
//                }
//            }
//
//        }
//        return false;
//    }

    public static Boolean canResult(List<Integer> data, int type){

        int blankId = ImageUtil.mBlankItemBean.getmItemId();

        if(data.size() % 2 == 1){

            return getInversions(data) % 2 == 0;
        }

        else{

            Log.d("GameUtil","空白位于的行数   "+(blankId - 1)/type + "空白图的ID   "+ blankId);

            if (((blankId - 1)/type) % 2 == 1){

                return getInversions(data) % 2 == 0;
            }else{

                Log.d("GameUtil","位于偶数行，倒置和是   "+ getInversions(data));
                return getInversions(data) % 2 == 1;
            }

        }


    }

    private static int getInversions(List<Integer> data) {

        int inversions = 0;
        int inversionCount = 0;
        for (int i = 0; i <data.size();i++){

            for (int j = i + 1; j < data.size(); j++){

                int index = data.get(i);
                if(data.get(j) != 0 && data.get(j) < index){

                    inversionCount++;
                }
            }
            inversions += inversionCount;

            inversionCount = 0;
        }

        return inversions;
    }


    //求得倒置和
    private static int getSum(List<Integer> data) {

        List<Integer> listText = new ArrayList();

        int sum = 0;

        for (int i = 0; i < data.size(); i++) {
            int count = 0;
            for (int j = i + 1; j < data.size(); j++) {

                if (data.get(i) > data.get(j) && data.get(j) != 0) {

                    count++;

                }

            }
            sum += count;
            listText.add(count);
        }

        Log.d("GameUtil", "//求得 data 数列" + data.toString());
        Log.d("GameUtil", "//求得倒置数列" + listText.toString());
        Log.d("GameUtil", "//求得倒置和" + sum + "");

        return sum;
    }


    //生成随机数(指定范围的)
    public static int getRandom(int max, int min) {
        Random random = new Random();
        int randnum = random.nextInt(max) % (max - min + 1) + min;

        // Log.d("PuzzleActivity", "---"+randnum);
        return randnum;

    }

    /**
     * 判断当前点击的图像是否可以移动
     *
     * @param position 当前点击出图像itemId
     */

    public static Boolean canRemove(int position, int type) {

        ItemBean blank = ImageUtil.mBlankItemBean;//当前封装空白图的对象

        Boolean flag = false;

        int itemId = blank.getmItemId() - 1;

        int space = Math.abs(itemId - position);

        //同行
        if (itemId / type == position / type && space == 1) {

            flag = true;

        }
        //同列
        else if (space == type) {

            flag = true;
        }

        return flag;
    }

    /**
     * 判断是否成功
     */

    public static Boolean isSuccess( List<ItemBean> itemBean, int type) {

        for (ItemBean itemBean1: itemBean){

            Log.d("GameUtil","ItemBean  BitmapId  "+itemBean1.getmBitmapId() +"ItemId   "+ itemBean1.getmItemId());

        }

        for (ItemBean it : itemBean) {

            if (it.getmBitmapId() != 0 && it.getmItemId() == it.getmBitmapId()) {

                continue;
            } else if (it.getmBitmapId() == 0 && it.getmItemId() == type * type) {

                continue;
            } else {

                return false;
            }

        }
        return true;
    }
}


